﻿Imports DAL
Public Class UnidadeBLL
    Dim ObjDAL As New UnidadeDAL
#Region "Propriedades"
    Property Codigo As Integer
    Property Descricao As String
#End Region

#Region "Construtores"
    Sub New()

    End Sub
    Sub New(ByVal codigo As Integer)
        carrega(codigo)
    End Sub
    Sub New(codigo As Integer, descricao As String)
        Me.Codigo = codigo
        Me.Descricao = descricao
    End Sub

#End Region

#Region "Métodos"
#Region "Inserir/Alterar"
    Function InserirAlterar(usuario As String) As Boolean
        Try
            ObjDAL = New UnidadeDAL
            If (Trim(Descricao) <> "") Then
                If (ObjDAL.ValidaReplica(Me.Codigo, Me.Descricao)) Then
                    Me.Codigo = ObjDAL.InserirAlterar(Me.Codigo, Me.Descricao, usuario)
                Else : Throw New Exception("Unidade já cadastrada")
                End If
            Else : Throw New Exception("Favor preencher os campos obrigatórios")
            End If
        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
        Return True
    End Function

#End Region

#Region "Excluir"
    Function Excluir() As Boolean
        Dim ret As Integer
        Try
            ObjDAL = New UnidadeDAL
            If (ObjDAL.ValidaExcluir(Me.Codigo)) Then
                ret = ObjDAL.Excluir(Codigo)
            Else
                Throw New Exception("Esta unidade não pode ser excluida, verifique antes a ocorrência da mesma no(s) formulário(s): Coleta")
            End If

        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
        If (ret = -1) Then
            Return False
        Else : Return True
        End If
    End Function

#End Region

#Region "Buscar"
    Function buscar(ByVal descricao As String, tipo As Integer) As DataTable
        Dim ret As DataTable
        Try
            ObjDAL = New UnidadeDAL
            ret = ObjDAL.buscar(descricao, tipo)
        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
        Return ret
    End Function
#End Region

#End Region

#Region "Carrega Propriedades"
    Sub carrega(ByVal codigo As Integer)
        Dim tab As DataTable
        Try
            ObjDAL = New UnidadeDAL
            tab = ObjDAL.buscar(codigo, 0)
            If (tab.Rows.Count > 0) Then
                Me.Codigo = Integer.Parse(tab.Rows(0)("uni_codigo").ToString)
                Me.Descricao = tab.Rows(0)("uni_descricao").ToString

            End If
        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
    End Sub
#End Region

#Region "Limpa Propriedades"
    Sub limpar()
        Me.Codigo = Nothing
        Me.Descricao = Nothing
    End Sub
#End Region
End Class
